package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.lovo.model.CommodityOrder;
import com.lovo.service.CommodityOrderService;
import com.lovo.service.impl.CommodityOrderServiceImpl;
import com.lovo.VO.CommodityOrderVo;
import com.lovo.VO.ServiceOrderVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

    @WebServlet(name = "ServletCommodityOrder",urlPatterns = "/ServletCommodityOrder.do")
public class ServletCommodityOrder extends HttpServlet {
    CommodityOrderService commodityOrderService=new CommodityOrderServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageSizeA=request.getParameter("pageSize");
        int pageSize=10;
        if(pageSizeA!=null){
            pageSize = Integer.parseInt(pageSizeA);
        }
        int pageNo = 1;
        String p = request.getParameter("pageNo");
        if(p==null){
            pageNo=0;
        }else{
            pageNo=Integer.parseInt(p);
        }
        String method=request.getParameter("method");
        System.out.println("method = " + method);
        if(method.equals("selectAll")){
            select(request,response,pageNo,pageSize);
        }
        if(method.equals("update")){
            update(request,response);
        }
        if(method.equals("selectById")){
            selectById(request,response);
        }
    }

    private void selectById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        ServiceOrderVo com=commodityOrderService.selectByPrimaryKey(id);
        ObjectMapper objectMapper=new ObjectMapper();
        response.getWriter().write(objectMapper.writeValueAsString(com));
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CommodityOrderVo vo=new CommodityOrderVo();
        String isId=request.getParameter("id");
        if(isId!=null){
            vo.setId(Integer.parseInt(isId));
        }
        vo.setStart(request.getParameter("start"));
        vo.setAddress(request.getParameter("address"));
        vo.setTel(request.getParameter("tel"));
        vo.setName(request.getParameter("name"));
        String isNumber=request.getParameter("number");
        if(isNumber!=null){
            vo.setNumber(Integer.parseInt(isNumber));
        }
        int n=commodityOrderService.updateByPrimaryKey(vo);
        if(n>0) {
            response.getWriter().write("true");
        }else response.getWriter().write("false");
    }

    private void select(HttpServletRequest request, HttpServletResponse response,int pageNo,int pageSize) throws IOException {
        CommodityOrderVo vo=new CommodityOrderVo();
        String isBy=request.getParameter("isBy");
        String idS=request.getParameter("id");
        if(idS!=null){
            vo.setId(Integer.parseInt(idS));
        }
        if(isBy!=null){
            if(isBy.equals("name")){
                vo.setName(request.getParameter("txt"));
            }
            if(isBy.equals("commodName")){
                vo.setCommodName(request.getParameter("txt"));
            }
            if(isBy.equals("start")){
                vo.setStart(request.getParameter("txt"));
            }
        }
        PageInfo<CommodityOrderVo> pageInfo=commodityOrderService.selectAll(vo,pageNo,pageSize);
        ObjectMapper objectMapper = new ObjectMapper();
        response.getWriter().write(objectMapper.writeValueAsString(pageInfo));
    }
}
